-- Tags: long

set optimize_read_in_order=1;
set enable_json_type=1;
set allow_experimental_variant_type=1;
set use_variant_as_common_type=1;

drop table if exists test;

{% for create_command in ['create table test (id UInt64, json JSON(a UInt32), data String, index idx1 json.a type minmax, index idx2 json.b::String type minmax, index idx3 json.a || json.b::String type minmax) engine=MergeTree order by tuple() settings min_rows_for_wide_part=100000000, min_bytes_for_wide_part=1000000000, index_granularity=1;',
                          'create table test (id UInt64, json JSON(a UInt32), data String, index idx1 json.a type minmax, index idx2 json.b::String type minmax, index idx3 json.a || json.b::String type minmax) engine=MergeTree order by tuple() settings min_rows_for_wide_part=1, min_bytes_for_wide_part=1, vertical_merge_algorithm_min_columns_to_activate=10, vertical_merge_algorithm_min_rows_to_activate=1000000000, index_granularity=1;',
                          'create table test (id UInt64, json JSON(a UInt32), data String, index idx1 json.a type minmax, index idx2 json.b::String type minmax, index idx3 json.a || json.b::String type minmax) engine=MergeTree order by tuple() settings min_rows_for_wide_part=1, min_bytes_for_wide_part=1, vertical_merge_algorithm_min_columns_to_activate=1, vertical_merge_algorithm_min_rows_to_activate=1, index_granularity=1;'] -%}

select '{{ create_command }}';

{{ create_command }}

system stop merges test;
insert into test select number, toJSONString(map('a', number, 'b', 'str_' || number, 'c', range(number % 10))), 'Hello, World!' from numbers(4);
insert into test select number, toJSONString(map('a', number, 'b', 'str_' || number, 'c', range(number % 10))), 'Hello, World!' from numbers(4, 4);

select * from test where json.a < 2 order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where json.a < 2) where explain not like '%Expression%' and explain not like '%Filter%';
select * from test where json.b::String < 'str_2' order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where json.b::String < 'str_2') where explain not like '%Expression%' and explain not like '%Filter%';
select * from test where json.a < 2 and json.b::String < 'str_1' order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where json.a < 2 and json.b::String < 'str_1') where explain not like '%Expression%' and explain not like '%Filter%';
select * from test where json.a || json.b::String < '2str_2' order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where json.a || json.b::String < '2str_2') where explain not like '%Expression%' and explain not like '%Filter%';

system start merges test;
optimize table test final;
select count() from system.parts where active and table = 'test' and database = currentDatabase();


select * from test where json.a < 2 order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where json.a < 2) where explain not like '%Expression%' and explain not like '%Filter%';
select * from test where json.b::String < 'str_2' order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where json.b::String < 'str_2') where explain not like '%Expression%' and explain not like '%Filter%';
select * from test where json.a < 2 and json.b::String < 'str_1' order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where json.a < 2 and json.b::String < 'str_1') where explain not like '%Expression%' and explain not like '%Filter%';
select * from test where json.a || json.b::String < '2str_2' order by id;
select trimLeft(explain) from (explain indexes=1 select * from test where json.a || json.b::String < '2str_2') where explain not like '%Expression%' and explain not like '%Filter%';

drop table test;

{% endfor -%}
